<?php

namespace common\models\goods\cate;

use common\models\BaseModel;

/**
 * Desc 商品分类模型
 */
class GoodsCateModel extends BaseModel
{

    /** @var int 绑定状态: 启用 */
    const STATE_ENABLE = 1;
    /** @var int 绑定状态: 禁用 */
    const STATE_DISABLE = 2;

    public static $state = [
        self::STATE_ENABLE => '启用',
        self::STATE_DISABLE => '禁用',
    ];


    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%goods_category}}';
    }


    /**
     * @desc 递归获取树状菜单
     * @return string
     * */
    public static function arrayTree($ary = [], $route_parent_id = 0, $level = 10, $i = 1)
    {
        $arr = [];
        foreach ($ary as &$rs) {
            if ($rs['pid'] != $route_parent_id) {
                continue;
            }
            if ($i > $level) {
                break;
            }

            $arr[$rs['id']] = $rs;
            $n = $i;
            $n++;
            $children = self::arrayTree($ary, $rs['id'], $level, $n);
            if ($children) {
                $arr[$rs['id']]['children'] = $children;
            } else {
                $arr[$rs['id']]['children'] = [];
            }
        }
        if ($arr) {
            $arr = array_values($arr);
        }
        return $arr;
    }
}
